#ifndef SAFEHASHMAP_H_
#define SAFEHASHMAP_H_

#include <hash_map>

#include "Functor.h"
#include "Common\stragy\threadsafe\CriticalLock.h"

namespace FURY
{
	namespace CONTAINER
	{
		using FURY::STRAGY::CriticalLock;

		template<typename K, typename T>
		class SafeHashMap
		{
			typedef stdext::hash_map<K, T>	HashMapContainer;
			HashMapContainer	m_container;
			CriticalLock	m_lock;
		public:
			SafeHashMap(void)	{};
			~SafeHashMap(void)	{};

		public:
			void Insert(K key, T obj)
			{
				m_container.insert(std::make_pair(key, obj));
			};

			bool Has(K key)
			{
				if (m_container.find(key) == m_container.end())
					return false;
				else
					return true;
			};

			T& operator[](K key)
			{
				return m_container[key];
			};

			bool Erase(K key)
			{
				HashMapContainer::iterator iter = m_container.find(key);
				if (iter == m_container.end())
					return false;
				else
				{
					m_container.erase(iter);
					return true;
				}
			};

			void Clear(void)
			{
				m_container.clear();
			};

			bool Empty(void)
			{
				return m_container.empty();
			};

			int GetSize(void)
			{
				return m_container.size();
			}

			void ForEach(Functor<T> *func)
			{
				for (HashMapContainer::iterator iter = m_container.begin(); iter != m_container.end(); iter++)
					(*func)(iter->second);
			};

			operator CriticalLock&()
			{
				return m_lock;
			};
		};
	};
};

#endif // SAFEHASHMAP_H_
